package hlhj.fhp.supreme.activitys

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.ChangeAddressPopwindow
import hlhj.fhp.supreme.customview.CityPop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_edit_location_aty.*

class EditLocationAty : BaseAty(), CityPop.OnResult {

    private var cid=""
    private var pid=""
    private var aid=""

    override fun getContentId(): Int {
        return R.layout.activity_edit_location_aty
    }

    override fun initView() {
        when(        intent.getIntExtra("type",0)){
            1->{
                tvTittle.text = "新增地址"
            }
            else->{
                tvTittle.text="编辑地址"
            }
        }
        if (intent.getStringExtra("name")!=null)etName.setText(intent.getStringExtra("name"))
        if (intent.getStringExtra("phone")!=null)etPhone.setText(intent.getStringExtra("phone"))
        if (intent.getStringExtra("loc")!=null) tvLocation.text = intent.getStringExtra("loc")
        if (intent.getStringExtra("detail")!=null)etDetail.setText(intent.getStringExtra("detail"))
    }

    override fun initListener() {

        btChooseCity.setOnClickListener {
            val cityPop = CityPop(this, this)
            cityPop.showAtLocation(father,Gravity.BOTTOM,0,0)
        }
        btExit.setOnClickListener {
            finish()
        }

        btSave.setOnClickListener {
            if (etName.text.isEmpty()||etPhone.text.isEmpty()||tvLocation.text.isEmpty()||etDetail.text.isEmpty()){
                MyUtils.toast("请完善信息")
            }else{
                if (intent.getIntExtra("type",0)==1){
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.EDIT_LOCATION)
                            .params("mobile",etPhone.text.toString())
                            .params("name",etName.text.toString())
                            .params("province_id",cid)
                            .params("city_id",pid)
                            .params("area_id",aid)
                            .params("address",etDetail.text.toString())
                            .execute(object :JsonCallBack<BaseBean>(this){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("添加成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }else{
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.EDIT_LOCATION)
                            .params("mobile",etPhone.text.toString())
                            .params("name",etName.text.toString())
                            .params("province_id",cid)
                            .params("city_id",pid)
                            .params("area_id",aid)
                            .params("id",intent.getIntExtra("id",-1))
                            .params("address",etDetail.text.toString())
                            .execute(object :JsonCallBack<BaseBean>(this){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }

            }
        }
    }
    override fun onResult(cTv: String, pTv: String, aTv: String, cid: String, pid: String, aId: String) {
        tvLocation.text=cTv+"-"+pTv+"-"+aTv
        this.cid=cid
        this.pid=pid
        this.aid=aId
    }
}
